<template>
    <div class="container">
        <div class="header_wrap">
            <div class="left">
                <i class="el-icon-edit-outline"></i> 查看
            </div>

            <div class="right">
                <el-button @click="handleAddOrUpdateVisible()" type="primary" size="medium" icon="el-icon-plus" :disabled="!searchForm.menuId">新增</el-button>
                <el-button @click="handleDelete()" type="danger" size="medium" icon="el-icon-delete" :disabled="selectionRows.length == 0" >删除</el-button>
            </div>

        </div>

        <div class="wrap">
            <!-- 表格 -->
            <el-table
                ref="table"
                size="medium"
                empty-text="暂无数据"
                element-loading-text="给我一点时间"
                fit border highlight-current-row
                :stripe="stripe"
                v-loading="loadingData"
                :data="dataList"
                @selection-change="handleSelection">

                <el-table-column type="selection" width="55" prop="selection" v-if="isHideColumn('selection')" align="center"></el-table-column>
                <el-table-column label="序号" type="index" prop="index" v-if="isHideColumn('index')" width="60" align="center" />
                <el-table-column label="权限名称" prop="name" v-if="isHideColumn('name')" align="left" show-overflow-tooltip/>
                <el-table-column label="权限标识" prop="permission" v-if="isHideColumn('permission')" align="left" show-overflow-tooltip/>
                <el-table-column label="权限类型" prop="type" v-if="isHideColumn('type')" align="center" width="100px" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.type == 1">页面</el-tag>
                        <el-tag type="danger" size="small" v-else>按钮</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remarks" v-if="isHideColumn('remarks')" align="left" show-overflow-tooltip/>

                <el-table-column label="操作" prop="btn" v-if="isHideColumn('btn')" width="200px" align="left" fixed="right">
                    <template slot-scope="scope">
                        <el-button @click="handleAddOrUpdateVisible(scope.row.id)" type="text" size="small">编辑</el-button>
                        <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                </el-table-column>

            </el-table>

            <div class="page">
                <!-- 分页 -->
                <TablePagination
                    :total.sync="total"
                    :page.sync="searchForm.page"
                    :limit.sync="searchForm.limit"
                    @handleSizeChange="handleSizeChange"
                    @handleCurrentChange="handleCurrentChange"/>
            </div>

        </div>

        <!-- 弹窗, 新增 / 修改 -->
        <AddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></AddOrUpdate>

    </div>
</template>

<script>
import TableMixin from '@/mixin/TableMixin'
import TablePagination from '@/views/components/table/table-pagination'
import AddOrUpdate from './add-or-update-dialog'
export default {
    mixins: [TableMixin],
    components: {
        TablePagination,
        AddOrUpdate
    },

    data() {
        return {
            dataList: [],
            total: 0,
            loadingData: false,
            addOrUpdateVisible: false,
            selectionRows: [],
            searchFlag: true,
            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: null,
                sortType: null,
                keyword: null,
                menuId: null
            }
        }
    },

    created() {
    },

    mounted() {
        this.$nextTick(() => {
        })
    },

    methods: {

        menus(id){
            this.searchForm.menuId = id
            this.getDataList()
        },

        /**
         * 新增或编辑
         */
        handleAddOrUpdateVisible(id) {
            // 表单窗口
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id, this.searchForm.menuId)
            })
        },

        /**
         * 删除
         */
        async handleDelete(row) {
            let ids = []
            if (row) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {
                const confirm = await this.$confirm('确定要删除？', '提示', { type: 'warning' }).catch(() => {})
                if(confirm) {
                    const res = await this.$http({
                        url: '/system/permission/delete',
                        method: 'POST',
                        data: ids
                    })
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                }
            }
        },

        /**
         * 获取表格数据
         */
        async getDataList() {
            this.loading = true
            const res = await this.$http({
                url: '/system/permission/page',
                method: 'GET',
                params: this.searchForm
            })
            if (this.ResultMessage(res, false)) {
                this.dataList = res.data.records
                this.total = res.data.total
            }
            this.loading = false
        },

        /**
         * 搜索
         */
        handleSearch() {
            this.getDataList()
        },
        /**
         * 重新加载数据表格
         */
        handleReload() {
            this.getDataList()
        },
        /**
         * 重置搜索表单
         */
        handleReset() {
            this.$refs.searchForm1.resetFields()
            this.$refs.searchForm2.resetFields()
        }

    }

}
</script>
<style lang="scss">

</style>
<style lang="scss" scoped>
.header_wrap{
    width: 100%;
    height: 35px;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content:space-between;

    border-bottom: 1px solid #ddd;
    font-size: 16px;

    .right{
        margin-right: 20px;
    }

    .el-icon-edit-outline{
        font-size: 18px;
        margin-right: 5px;
    }
    .btn_ext{
        margin-left: 10px;
    }
}

.wrap{
    padding: 10px;
    .page{
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 10px;
    }
    .tips{

    }

    .form_wrap{
        padding: 10px 0;
        .bts{
            margin-left: 120px;
        }
    }
}


</style>
